/*!
	@file
	@author		Albert Semenov
	@date		12/2009
*/

#include "MyGUI_EggRTTexture.h"
#include "MyGUI_EggRenderManager.h"
#include "MyGUI_EggDiagnostic.h"

namespace MyGUI{

	EggRTTexture::EggRTTexture(egg::TexturePtr eggTexture){
        using namespace egg;
        float width=eggTexture->getWidth();
        float height=eggTexture->getHeight();
		mRenderTargetInfo.maximumDepth = 1.0f;
		mRenderTargetInfo.hOffset = 0;
		mRenderTargetInfo.vOffset = 0;
		mRenderTargetInfo.aspectCoef = height/ width;
		mRenderTargetInfo.pixScaleX = 1.0f / width;
		mRenderTargetInfo.pixScaleY = 1.0f / height;
        DepthBufferPtr depth(new DepthBuffer(width,height,false));
        mEggFrameBuffer=FrameBufferPtr(new FrameBuffer(width,height));
        mEggFrameBuffer->setDepthBuffer(depth);
        mEggFrameBuffer->attachColor(eggTexture);
	}

	EggRTTexture::~EggRTTexture(){
	}

	void EggRTTexture::begin(){
        egg::StateCache::setFrameBuffer(mEggFrameBuffer->getID());
		glViewport(0, 0, mEggFrameBuffer->getWidth(), mEggFrameBuffer->getHeight());
		EggRenderManager::getInstance().begin();
		glClear(GL_COLOR_BUFFER_BIT);
	}

	void EggRTTexture::end(){
		EggRenderManager::getInstance().end();
		egg::StateCache::setFrameBuffer(0);
	}

	void EggRTTexture::doRender(IVertexBuffer* _buffer, ITexture* _texture, size_t _count){
		EggRenderManager::getInstance().doRender(_buffer, _texture, _count);
	}

} // namespace MyGUI
